package com.alliky.core.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alliky.core.entity.MessageEvent;
import com.alliky.core.loader.Loader;
import com.alliky.core.widget.RxToast;
import com.trello.rxlifecycle2.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.ref.WeakReference;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import es.dmoral.toasty.Toasty;

/**
 * @Description: DOTO
 * @Author: wxianing
 * @CreateDate: 2019/9/29 17:18
 */
public abstract class Fragment<P extends BasePresenter> extends RxFragment implements IView {

    //上下文
    public WeakReference<Context> mContext;
    public P mPresenter;
    private Unbinder mUnbinder;

    protected int pageNum = 1;
    protected int pageSize = 8;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View rootView;
        if (setLayout() instanceof Integer) {
            rootView = inflater.inflate((int) setLayout(), container, false);
        } else if (setLayout() instanceof View) {
            rootView = (View) setLayout();
        } else {
            throw new ClassCastException("type of setLayout() must be int or View!");
        }

        mPresenter = createPresenter();
        mContext = new WeakReference<>(getActivity());
        if (mPresenter != null) {
            mPresenter.injectLifecycleProvider(this);
        }

        mUnbinder = ButterKnife.bind(this, rootView);

        onInitView(savedInstanceState, rootView);

        onInitData();

        onInitEvent();

        return rootView;
    }

    protected abstract P createPresenter();

    public abstract Object setLayout();

    public abstract void onInitView(Bundle savedInstanceState, View rootView);

    public void initEventBus() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    /**
     * 初始化加载数据
     */
    public void onInitData() {
    }

    /**
     * 初始化时间处理
     */
    public void onInitEvent() {
    }

    public void onViewClick(View v) {
    }

    /**
     * 获取上下文
     *
     * @return
     */
    public Context getContext() {
        return mContext.get();
    }

    public void showToast(String message) {
        Toasty.normal(getContext(), message).show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MessageEvent message) {

    }

    public boolean isBackPressed() {
        return false;
    }

    /**
     * 页面跳转
     *
     * @param clazz
     */
    public void startActivity(Class<?> clazz) {
        startActivity(clazz, null);
    }

    /**
     * 携带数据跳转
     *
     * @param clazz
     * @param bundle
     */
    public void startActivity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), clazz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class打开编辑界面
     *
     * @param cls
     * @param bundle
     * @param requestCode
     */
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

        if (mPresenter != null) {
            mPresenter.detachView();
        }
    }

    @Override
    public void showLoading() {
        Loader.showLoading(getContext());
    }

    @Override
    public void hideLoading() {
        Loader.stopLoading();
    }

    @Override
    public void showError(int code, String message) {
        RxToast.error(message);
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void finishLoadMore() {

    }

    @Override
    public void showMessage(String message) {

    }

    // 两次点击按钮之间的点击间隔不能少于2000毫秒
    private final int MIN_CLICK_DELAY_TIME = 2000;
    // 上次点击时间
    private long lastClickTime;
    //上次点击的viewId
    private int lastClickId = 0;

    //限制全局点击事件
    public boolean isFastClick(int viewId) {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if (lastClickId == viewId) {
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                flag = true;
            }
        }
        lastClickTime = curClickTime;
        lastClickId = viewId;
        return flag;
    }
}
